package com.totoro.tyrank.inventory;

import com.totoro.tyrank.TyRank;
import com.totoro.tyrank.constants.InvRankTypeConst;
import com.totoro.tyrank.constants.NBTKeyConst;
import com.totoro.tyrank.data.RankingData;
import com.totoro.tyrank.handle.PaginatorHandler;
import com.totoro.tyrank.utils.*;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 主要所有排行榜GUI界面
 */
public class CurrentRankInventory {

    public static Map<UUID, Integer> currentPage = new ConcurrentHashMap<>();

    public static void openMainInv(Player player) {
        if(! currentPage.containsKey(player.getUniqueId())) {
            currentPage.put(player.getUniqueId(), 1);
        }

        Inventory inv = Bukkit.createInventory(null, 36, TyRank.getLanguageManager().getLanguageData().getGui().getCurrentRankInvTitle());
        ItemStack gridPlateItem = ItemStackUtil.getGridPlateItem();
        RankInvUtil.setUnifiedRankTypeItemStack(inv, InvRankTypeConst.CURRENT_RANK_INV);

        inv.setItem(18, gridPlateItem);
        inv.setItem(26, gridPlateItem);
        for (int i = 28; i < 35; i++) {
            inv.setItem(i, gridPlateItem);
        }

        // 排行榜设置，启动分页器
        PaginatorHandler<RankingData> currentRankPaginatorHandler = TyRank.getRankingManager().getCurrentRankPaginatorHandler();
        List<RankingData> rankingDataList = currentRankPaginatorHandler.getPage(currentPage.get(player.getUniqueId()));
        // 排行数据是从19开始的
        int rankDataSlot = 19;
        for (RankingData rankingData : rankingDataList) {
            inv.setItem(rankDataSlot, ItemStackUtil.getRankItemStackByRankingData(player, rankingData));
            rankDataSlot ++;
        }
        // 上一页27 和下一页35 设置
        ItemStack prePageItem = new ItemStack(Material.FEATHER, currentPage.get(player.getUniqueId()), (short)0);
        ItemMeta prePageItemMeta = prePageItem.getItemMeta();
        prePageItemMeta.setDisplayName(ColorUtil.color("当前页数:" + currentPage.get(player.getUniqueId())));
        prePageItemMeta.setLore(Stream.of(ColorUtil.color("&7左键点击前往上一页"),ColorUtil.color("&f右键点击前往第一页")).collect(Collectors.toList()));
        prePageItem.setItemMeta(prePageItemMeta);
        prePageItem = NBTUtil.addNBT(prePageItem, NBTKeyConst.CURRENT_PAGE, currentPage.get(player.getUniqueId()).toString());
        prePageItem = NBTUtil.addNBT(prePageItem, NBTKeyConst.TOTAL_PAGE, currentRankPaginatorHandler.getTotalPages().toString());
        inv.setItem(27, prePageItem);

        ItemStack nextPageItem = new ItemStack(Material.FEATHER, currentPage.get(player.getUniqueId()), (short)0);
        ItemMeta nextPageItemMeta = nextPageItem.getItemMeta();
        nextPageItemMeta.setDisplayName(ColorUtil.color("当前页数:" + currentPage.get(player.getUniqueId())));
        nextPageItemMeta.setLore(Stream.of(ColorUtil.color("&7左键点击前往下一页"),ColorUtil.color("&f右键点击前往最后一页")).collect(Collectors.toList()));
        nextPageItem.setItemMeta(nextPageItemMeta);
        nextPageItem = NBTUtil.addNBT(nextPageItem, NBTKeyConst.CURRENT_PAGE, currentPage.get(player.getUniqueId()).toString());
        nextPageItem = NBTUtil.addNBT(nextPageItem, NBTKeyConst.TOTAL_PAGE, currentRankPaginatorHandler.getTotalPages().toString());
        inv.setItem(35, nextPageItem);

        // 打开
        player.openInventory(inv);
    }

    /**
     * 点击切换上一页
     * @param player
     */
    public static void preCurrentPage(Player player) {
        Integer page = currentPage.getOrDefault(player.getUniqueId(), 1);
        if(page == 1) {
            player.sendMessage(TyRank.getConfigManager().getConfigData().getPrefix() +
                    TyRank.getLanguageManager().getLanguageData().getMsg().getAlreadyFirstPage());
        } else if (page < 1) {
            currentPage.put(player.getUniqueId(), 1);
            player.sendMessage(TyRank.getConfigManager().getConfigData().getPrefix() +
                    TyRank.getLanguageManager().getLanguageData().getMsg().getAlreadyFirstPage());
            openMainInv(player);
        } else {
            currentPage.put(player.getUniqueId(), currentPage.get(player.getUniqueId()) - 1);
            openMainInv(player);
        }
    }

    /**
     * 点击切换到第一页
     * @param player
     */
    public static void firstCurrentPage(Player player) {
        currentPage.put(player.getUniqueId(), 1);
        player.sendMessage(TyRank.getConfigManager().getConfigData().getPrefix() +
                TyRank.getLanguageManager().getLanguageData().getMsg().getChangeFirstPage());
        openMainInv(player);
    }

    /**
     * 切换下一页
     * @param player
     */
    public static void nextCurrentPage(Player player) {
        PaginatorHandler<RankingData> currentRankPaginatorHandler = TyRank.getRankingManager().getCurrentRankPaginatorHandler();
        // 获取总页数
        int totalPages = currentRankPaginatorHandler.getTotalPages();
        Integer page = currentPage.getOrDefault(player.getUniqueId(), 1);
        if(page == totalPages) {
            player.sendMessage(TyRank.getConfigManager().getConfigData().getPrefix() +
                    TyRank.getLanguageManager().getLanguageData().getMsg().getAlreadyLastPage());
        } else if (page > totalPages) {
            player.sendMessage(TyRank.getConfigManager().getConfigData().getPrefix() +
                    TyRank.getLanguageManager().getLanguageData().getMsg().getAlreadyLastPage());
            currentPage.put(player.getUniqueId(), totalPages);
            openMainInv(player);
        } else {
            currentPage.put(player.getUniqueId(), currentPage.get(player.getUniqueId()) + 1);
            openMainInv(player);
        }
    }

    /**
     * 点击切换到最后一页
     * @param player
     */
    public static void lastCurrentPage(Player player) {
        PaginatorHandler<RankingData> currentRankPaginatorHandler = TyRank.getRankingManager().getCurrentRankPaginatorHandler();
        // 获取总页数
        int totalPages = currentRankPaginatorHandler.getTotalPages();
        currentPage.put(player.getUniqueId(), totalPages);
        player.sendMessage(TyRank.getConfigManager().getConfigData().getPrefix() +
                TyRank.getLanguageManager().getLanguageData().getMsg().getChangeLastPage());
        openMainInv(player);
    }

}
